package cn.darkhorse.basic.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.darkhorse.basic.mapper.QmsSignMapper;
import cn.darkhorse.basic.domain.QmsSign;
import cn.darkhorse.basic.service.IQmsSignService;

/**
 * 签名管理Service业务层处理
 *
 * @author ytq
 * @date 2024-07-18
 */
@Service
public class QmsSignServiceImpl extends ServiceImpl<QmsSignMapper, QmsSign> implements IQmsSignService
{
    @Autowired
    private QmsSignMapper qmsSignMapper;
    @Autowired
    private ISysDeptService deptService;
    /**
     * 查询签名管理
     *
     * @param id 签名管理主键
     * @return 签名管理
     */
    @Override
    public QmsSign selectQmsSignById(Long id)
    {
        QmsSign sign = getById(id);
        if (sign != null){
            SysDept sysDept = deptService.lambdaQuery().eq(SysDept::getDeptId,sign.getDeptId()).one();
            if (sysDept!=null){
                sign.setDeptName(sysDept.getDeptName());
            }
        }
        return sign;
    }

    /**
     * 查询签名管理列表
     *
     * @param qmsSign 签名管理
     * @return 签名管理
     */
    @Override
    public List<QmsSign> selectQmsSignList(QmsSign qmsSign)
    {
        QueryWrapper<QmsSign> wrapper = new QueryWrapper<>();
        if (qmsSign.getSignName()!=null){
            wrapper.like("sign_name",qmsSign.getSignName());
        }
        wrapper.orderByDesc("create_time");
        List<QmsSign> list = list(wrapper);
        for (QmsSign sign : list) {
            SysDept sysDept = deptService.lambdaQuery().eq(SysDept::getDeptId,sign.getDeptId()).one();
            if (sysDept!=null){
                sign.setDeptName(sysDept.getDeptName());
            }
        }
        return list;
    }

    /**
     * 新增签名管理
     *
     * @param qmsSign 签名管理
     * @return 结果
     */
    @Override
    public int saveQmsSign(QmsSign qmsSign)
    {

        String username = qmsSign.getUsername();
        if (!StringUtils.isEmpty(username)){
            if (qmsSign.getId()!=null){
                Long count = lambdaQuery()
                        .eq(QmsSign::getUsername, username).ne(QmsSign::getId, qmsSign.getId()).count();
                if (count>0){
                    throw new ServiceException("用户签名已存在");
                }
            }else {
                Long count = lambdaQuery().eq(QmsSign::getUsername, username).count();
                if (count>0){
                    throw new ServiceException("用户签名已存在");
                }
            }
        }
        if (qmsSign.getId() == null){
            if (StringUtils.isEmpty(qmsSign.getUsername())){
                throw new ServiceException("用户名不能为空");
            }
            qmsSign.setCreateTime(DateUtils.getNowDate());
            if (qmsSign.getSignPassword() == null || qmsSign.getSignPassword().equals("")){
                qmsSign.setSignPassword("123456");
            }
            return qmsSignMapper.insertQmsSign(qmsSign);
        }else{
            return qmsSignMapper.updateQmsSign(qmsSign);
        }
    }


    /**
     * 批量删除签名管理
     *
     * @param ids 需要删除的签名管理主键
     * @return 结果
     */
    @Override
    public int deleteQmsSignByIds(Long[] ids)
    {
        return qmsSignMapper.deleteQmsSignByIds(ids);
    }

    /**
     * 删除签名管理信息
     *
     * @param id 签名管理主键
     * @return 结果
     */
    @Override
    public int deleteQmsSignById(Long id)
    {
        return qmsSignMapper.deleteQmsSignById(id);
    }

    @Override
    public boolean resetPassword(QmsSign qmsSign) {
        QmsSign byId = null;
        if (qmsSign.getId() != null){
            byId = getById(qmsSign.getId());
        }else {
            String username = SecurityUtils.getUsername();
            if (username != null){
                byId = lambdaQuery().eq(QmsSign::getUsername, username).one();
            }
        }

        if (byId != null){
//            if (byId.getUsername()!=null){
//                if (SecurityUtils.getUsername().equals(byId.getUsername()) && !SecurityUtils.hasRole("")) {
//
//                }else {
//                    throw new RuntimeException("不可操作非本人签名");
//                }
//            }
            if (byId.getSignPassword().equals(qmsSign.getOldPassword())){
                qmsSign.setId(byId.getId());
                return updateById(qmsSign);
            }else {
                throw new RuntimeException("原密码输入有误!");
            }
        }
        return false;
    }
}
